package com.zshield.entry;

import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import com.zshield.util.Etime;

import java.time.LocalDateTime;
import java.util.Set;

public class FileSystemEntry implements EntryInterface<FileSystemEntry> {
    @SerializedName("SENSOR_ID")
    private String sensorId;
    @SerializedName("FORMAT")
    private String format;
    @SerializedName("OS")
    private String os;
    @SerializedName("FILE_EXT")
    private String fileExt;
    @SerializedName("ACCESS_FORMAT")
    private String accessFormat;
    @SerializedName("FILE_SIZE")
    private long fileSize;
    @SerializedName("ACCESS_FILE_SIZE")
    private long accessFileSize;
    @SerializedName("ACCESS_TIMES")
    private int accessTime;
    @SerializedName("EXE_NAME")
    private String exeName;
    @SerializedName("USERNAME_EX")
    private String usernameEx;
    @SerializedName("OBSOLETE")
    private int obsoLete;
    @SerializedName("TIME")
    private String time;
    @SerializedName("@timestamp")
    private String timeStamp;
    @SerializedName("DAY_TIME")
    private String dayTime;
    @SerializedName("TIME_hms")
    private String timeHms;
    @SerializedName("PTYPE")
    private String ptype;

    public FileSystemEntry (JsonObject obj,String fileSysAction,Set<String> accessSet,String accFormat) {
        sensorId = obj.get("SENSOR_ID").getAsString();
        format = "PRE_COMPUTE";
        os = obj.get("OS").getAsString();
        String ext = obj.get("FILE_EXT").getAsString();
        fileExt = ext.contains(",") ? ext.split(",")[0] : ext;
        fileSize = obj.get("FILE_SIZE").getAsLong();
        accessFormat = accFormat;
        accessFileSize = 0;
        accessTime = 0;
        exeName = obj.get("EXE_NAME").getAsString();
        usernameEx = obj.get("USERNAME_EX").getAsString();
        obsoLete = 0;
        String logTime = obj.get("TIME").getAsString();
        Etime etime = new Etime();
        time = etime.zoneParse(logTime);
        LocalDateTime ldt = LocalDateTime.parse(time, Etime.DATETIME_FORMATTER);
        dayTime = ldt.format(Etime.DATE_FORMATTER);
        timeStamp = LocalDateTime.now().format(Etime.DATETIME_FORMATTER);
        timeHms = ldt.format(Etime.TIME_FORMATTER);
        ptype = "Filesys";

        if (obj.has("AFTER_OPERATE_SIZE")) {
            fileSize = obj.get("AFTER_OPERATE_SIZE").getAsLong();
        }

        if (fileSysAction.equals("Read") || fileSysAction.equals("Write")){
            accessFileSize = obj.get("OPERATE_SIZE").getAsLong();
        }

        if (fileSysAction.equals("Create") || fileSysAction.equals("Copy")){
            accessFileSize = obj.get("FILE_SIZE").getAsLong();
        }

        if (fileSysAction.equals("Delete")){
            obsoLete = 1;
        }

        if (accessSet.contains(fileSysAction)) {
            accessTime = 1;
        }
    }

    public void update(FileSystemEntry that){
        if (that.fileSize > 0){
            this.fileSize = that.fileSize;
        }
        this.accessFileSize = this.accessFileSize + that.accessFileSize;
        this.accessTime = this.accessTime + that.accessTime;
        this.obsoLete = that.obsoLete;
        this.time = that.time;
    }

    @Override
    public String getDayTime() {
        return this.dayTime;
    }

    public String getDocId(){
        return "Filesys~" + sensorId + "~" + os + "~" + usernameEx + "~" + exeName + "~" + accessFormat + "~" + fileExt + "~" + dayTime;
    }

}
